package com.witframework.components.tag;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.witframework.WitException;

/**
 * @author wuxinyang
 * Jun 4, 2009 9:37:20 AM
 * 
 */
public class DataSetAuthSetDialogTag extends BodyTagSupport{
	
	private String id;//对话框的标题	
	private String dialogTitle;//对话框的标题
	private String width;//对话框宽度
	private String height;//对话框高度	
	
	private String data;//数据源，可以是实体的类名或数据库的表名，或是sql查询语句
	private String data2;
	private String properties;//在列表中显示的字段
	private String category;//实体分类
	private String entryid;//权限项标识
	private String entryname;//权限项名称	
	private String datatype="class";
	private String datastyle="table";
	private String root;
	private String parentid;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性	
	private String parentid2;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性	
	private String nodeid;//对应的熟悉名称(树节点的ID)，对于PAGE类型则忽略该属性
	private String nodeid2;	
	private String nodelabel;//上面对应的数据字段名称(树节点的label文本)，对于PAGE类型则忽略该属性
	private String nodelabel2;	
	private String titles;//列表的字段标题,如："name|实体名称,comment|说明,catalog|分类"，对于PAGE类型则忽略该属性
	private String multiple="0";//是否允许多选
	private String onlyLeaf="1";
	
	private String js;
	private String target;
	private String target_value;	
	
	@Override
	public int doAfterBody() throws JspException {		
		if((datatype==null||datatype.equalsIgnoreCase("table")||datatype.equalsIgnoreCase("sql"))
			&&(category==null)){
			throw new WitException("非page类型的数据源时,必须设置properties和category属性");
		}
		BodyContent body = getBodyContent();
	    String value = body.getString();
	    body.clearBody();
	    if (value == null || value.length() == 0)
            return SKIP_BODY;				
		StringBuffer param=new StringBuffer();			
		param.append("data=");	
		try {
			param.append(java.net.URLEncoder.encode(data,"utf-8"));
		} catch (UnsupportedEncodingException e1) {			
			param.append(data);
		}
		param.append("&id=").append(id);		
		param.append("&datatype=").append(datatype);		
		param.append("&properties=").append(properties);
		param.append("&category=").append(category);
		param.append("&entryid=").append(entryid);
		param.append("&entryname=").append(entryname);
		param.append("&datastyle=").append(datastyle);		
		param.append("&multiple=").append(multiple);
		param.append("&height=").append(height);		
		param.append("&width=").append(width);
		if(StringUtils.isNotEmpty(data2)){ 
			param.append("&data2=");
			try {
				param.append(java.net.URLEncoder.encode(data2,"utf-8"));
			} catch (UnsupportedEncodingException e1) {			
				param.append(data2);
			}
		}
		if(StringUtils.isNotEmpty(root)) param.append("&root=").append(root);
		if(StringUtils.isNotEmpty(parentid)) param.append("&parentid=").append(parentid);
		if(StringUtils.isNotEmpty(parentid2)) param.append("&parentid2=").append(parentid2);
		if(StringUtils.isNotEmpty(nodeid)) param.append("&nodeid=").append(nodeid);
		if(StringUtils.isNotEmpty(nodeid2)) param.append("&nodeid2=").append(nodeid2);
		if(StringUtils.isNotEmpty(nodelabel)) param.append("&nodelabel=").append(nodelabel);
		if(StringUtils.isNotEmpty(nodelabel2)) param.append("&nodelabel2=").append(nodelabel2);
		if(StringUtils.isNotEmpty(onlyLeaf)) param.append("&onlyLeaf=").append(onlyLeaf);
		
		if(StringUtils.isNotEmpty(target)) param.append("&target=").append(target);
		if(StringUtils.isNotEmpty(target_value)) param.append("&target_value=").append(target_value);
		
		if(StringUtils.isNotEmpty(titles)){
			param.append("&titles=");
			try {
				param.append(java.net.URLEncoder.encode(titles,"utf-8"));
			} catch (UnsupportedEncodingException e1) {			
				param.append(titles);
			}
		}
		try {
			getPreviousOut().print("<a href=\"#\" onclick=\""+(StringUtils.isNotEmpty(js)?"if("+js+")":"")+"showSelectWin('"+id+"','"+dialogTitle+"','dataSetAuthSetController.wit?"+param.toString()+"','',''"+(width==null?"":","+width)+(height==null?"":","+height)+")\">"+value+"</a>");
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		 return SKIP_BODY;
	}
	public void setData2(String data2) {
		this.data2 = data2;
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
	}

	public void setNodeid(String nodeid) {
		this.nodeid = nodeid;
	}

	public void setNodeid2(String nodeid2) {
		this.nodeid2 = nodeid2;
	}

	public void setNodelabel(String nodelabel) {
		this.nodelabel = nodelabel;
	}

	public void setNodelabel2(String nodelabel2) {
		this.nodelabel2 = nodelabel2;
	}

	public void setOnlyLeaf(String onlyLeaf) {
		this.onlyLeaf = onlyLeaf;
	}
	public void setData(String data) {
		this.data = data;
	}

	public void setProperties(String properties) {
		this.properties = properties;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public void setEntryid(String entryid) {
		this.entryid = entryid;
	}

	public void setEntryname(String entryname) {
		this.entryname = entryname;
	}

	

	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}

	public void setDatastyle(String datastyle) {
		this.datastyle = datastyle;
	}

	public void setTitles(String titles) {
		this.titles = titles;
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}

	public void setRoot(String root) {
		this.root = root;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setDialogTitle(String dialogTitle) {
		this.dialogTitle = dialogTitle;
	}
	public void setWidth(String width) {
		this.width = width;
		try {
			this.width=(String) ExpressionEvaluatorManager.evaluate("width",width,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
		
	}
	public void setHeight(String height) {
		this.height = height;
		try {
			this.height=(String) ExpressionEvaluatorManager.evaluate("height",height,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
	public void setParentid2(String parentid2) {
		this.parentid2 = parentid2;
	}
	public void setJs(String js) {
		this.js = js;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public void setTarget_value(String target_value) {
		this.target_value = target_value;
	}
}
